#include <Starfield.h>

#include <glut.h>




Starfield::Starfield(vec2 location, float W, float H, unsigned size, float margin)
{
	loc = location;
	w = W;
	h = H;
	const float fieldPaddingSize = margin;
	const unsigned STAR_FIELD_SIZE = size;

	for(unsigned i = 0; i < STAR_FIELD_SIZE; ++i)
	{
		float fieldSizeX = fieldPaddingSize + W;
		float fieldSizeY = fieldPaddingSize + H;

		float rx = Global::GetRandomFloat();
		float ry = Global::GetRandomFloat();
		stars.push_back( location + vec2( rx * fieldSizeX, ry * fieldSizeY ) );
	}
}


truth Starfield::IsNearby(vec2 pos, float nearDist)
{
	vec2 center = loc + vec2(w*0.5f, h*0.5f);
	if( (center - pos).Length() < nearDist )
		return 1;
	return 0;
}

void Starfield::Draw()
{
	glBegin(GL_POINTS);
	for(unsigned i = 0; i < stars.size(); ++i)
	{
		float r = 0.1f + 0.9f * Global::GetRandomFloat();
		glColor3f(r,r,r);
		glVertex2f( stars[i].x, stars[i].y );
	}
	glEnd();
}
	



std::vector<Starfield> MakeStarfields(unsigned numFields, float chunksize, unsigned starsPerChunk)
{
	std::vector<Starfield> starfields;
	for(unsigned j = 0; j < numFields; ++j)
	for(unsigned i = 0; i < numFields; ++i)
	{
		starfields.push_back( Starfield( vec2(i*chunksize,j*chunksize), chunksize, chunksize, starsPerChunk, 0 ) );
	}

	return starfields;
}